<template>
  <div class="page-container-padding">
    <!-- 查询条件 -->
    <div>
      <span>
        <el-date-picker
          v-model="dateRange"
          style="width: 220px"
          type="daterange"
          size="mini"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :clearable="false"
        ></el-date-picker>
      </span>
      <span style="margin-left: 6px">
        <el-select v-model="statisticsType" size="mini" style="width: 120px">
          <el-option :value="1" label="默认统计方式"></el-option>
          <el-option :value="2" label="小组统计方式"></el-option>
        </el-select>
      </span>
      <span style="margin-left: 6px;">
        <el-select size="mini" v-model="employeeType" style="width:120px">
          <el-option :value="1" label="全部人员"></el-option>
          <el-option :value="2" label="在职人员"></el-option>
          <el-option :value="3" label="离职人员"></el-option>
        </el-select>
      </span>
      <span style="margin-left: 6px;">
        <el-select size="mini" v-model="sortType" style="width:120px">
          <el-option
            v-for="item in sortTypes"
            :key="item.value"
            :value="item.value"
            :label="item.text"
          ></el-option>
        </el-select>
      </span>
      <span style="margin-left: 6px;"><el-button size="mini" type="primary" @click="queryTableData">查询</el-button></span>
      <span style="margin-left: 6px;"><el-button size="mini" type="success" @click="setCurrentYearDateRange">本年</el-button></span>
      <span style="margin-left: 6px;"><el-button size="mini" type="infor" @click="setForeYearDateRange">上一年</el-button></span>
      <span style="margin-left: 6px;"><el-button size="mini" type="infor" @click="setNextYearDateRange">下一年</el-button></span>
      <span style="margin-left: 6px;">
        <el-button
          size="mini"
          type="infor"
          @click="tabNum = tabNum === 0 ? 1 : 0">
          {{ tabNum === 0 ? '分月份统计' : '总统计表' }}
        </el-button>
      </span>
    </div>
    <!-- 统计情况表 -->
    <div v-show="tabNum === 0">
      <div style="margin-top:20px;">
        <div class="table-title">{{ tableTitle }}</div>
        <el-table border stripe :data="dataList" :empty-text="emptyDataDescription">
          <el-table-column prop="admin_name" label="姓名" width="70px"></el-table-column>
          <el-table-column prop="datalib_count" label="数据库" width="70px"></el-table-column>
          <el-table-column prop="datalib_own_rate" label="自采率" width="80px">
            <template slot-scope="scope">
              <span v-if="scope.row.datalib_own_rate">{{ (scope.row.datalib_own_rate * 100).toFixed(2)}}%</span>
            </template>
          </el-table-column>
          <el-table-column prop="short_message_count" label="短讯通" width="70px"></el-table-column>
          <el-table-column prop="strategy_count" label="投顾策略" width="80px"></el-table-column>
          <el-table-column prop="strategy_success_rate" label="成功率" width="80px">
            <template slot-scope="scope">
              <span v-if="scope.row.strategy_success_rate">{{ (scope.row.strategy_success_rate * 100).toFixed(2)}}%</span>
            </template>
          </el-table-column>
          <el-table-column prop="investment_plan_count" label="投资方案" width="80px"></el-table-column>
          <el-table-column prop="investment_plan_success_rate" label="成功率" width="80px">
            <template slot-scope="scope">
              <span v-if="scope.row.investment_plan_success_rate">{{ (scope.row.investment_plan_success_rate * 100).toFixed(2)}}%</span>
            </template>
          </el-table-column>
          <el-table-column prop="special_article_count" label="专题研究" width="140px"></el-table-column>
          <el-table-column prop="wechat_article_count" label="公众号文章" width="140px"></el-table-column>
          <el-table-column prop="media_article_count" label="媒体文章" width="140px"></el-table-column>
          <el-table-column prop="support_task_count" label="协同支持" width="150px"></el-table-column>
          <el-table-column prop="swiss_coin" label="瑞币收入" width="80px"></el-table-column>
          <el-table-column prop="lecture_count" label="演讲沙龙" width="140px"></el-table-column>
          <el-table-column prop="rank" label="排序" width="50px"></el-table-column>
        </el-table>
      </div>
      <!-- 注释 -->
      <div style="margin-top: 10px; font-size: 12px; line-height: 20px">
        注:
        <br />
        1、数据库与自采率不分时间范围，统计当前最新情况。
        <br />
        2、投顾策略与投资计划的数量、成功率的统计时间基准为该策略或方案的结束时间。
        <br />
        3、协同支持里包含了演讲沙龙的营业部类别的数据，即演讲沙龙不包含该类数据。
        <br />
        4、表中所有参与排序的人员数量为
        <span style="color: #36a3f0">{{ userCount }}</span>
        。
      </div>
    </div>
    <!-- 各模块统计表 -->
    <div v-show="tabNum === 1" style="margin-top: 20px">
      <div
        v-for="moduleItem in moduleStatisticList"
        :key="moduleItem.name"
        v-loading="!moduleItem.isLoaded"
      >
        <div class="work-name">{{ moduleItem.nameText }}</div>
        <el-table :border="true" :stripe="true" :data="moduleItem.tableData">
          <el-table-column
            v-for="colItem in moduleItem.tableColumns"
            :key="colItem.prop"
            :prop="colItem.prop"
            :label="colItem.label"
            width="110px"
          ></el-table-column>
        </el-table>
        <div class="work-tip">{{ moduleItem.tips }}</div>
      </div>
    </div>
  </div>
</template>

<script>
  import { datetimeToStr } from '@/utils/datetimeUtils'
  import { getDepartmentWorkStatistics } from '@/api/departmentWork/index'
  import { ssMonthly } from '@/api/departmentWork/strategy'
  import { SInvestmentMonthly } from '@/api/departmentWork/investment'
  import { sShortMsgMonthly } from '@/api/departmentWork/shortMessage'
  import { sTaskMonthly } from '@/api/departmentWork/specialTask'
  import { sArticleMontyly } from '@/api/departmentWork/articleWriting'
  import { sLectureMontyly } from '@/api/departmentWork/lecture'
  import { sOndutyMessageMonthly } from '@/api/departmentWork/ondutyMessage'
  export default {
    data() {
      return {
        tabNum: 0,
        showDatetime: '',
        timer: null,
        dateRange: [],
        tableTitle: '',
        userCount: 0,
        dataList: [],
        emptyDataDescription: '点击上方查询获取统计数据',
        // 统计方式1
        statisticsType: 1, // 1或2
        employeeType: 1, // 1,2,3
        sortType: 1,
        sortTypes: [
          { value: 1, text: '默认顺序' },
          { value: 2, text: '数据指标量' },
          { value: 3, text: '数据自采率' },
          { value: 4, text: '短讯通' },
          { value: 5, text: '投顾策略' },
          { value: 6, text: '策略成功率' },
          { value: 7, text: '投资方案' },
          { value: 8, text: '方案成功率' },
          { value: 9, text: '专题研究' },
          { value: 10, text: '公众号文章' },
          { value: 11, text: '媒体文章' },
          { value: 12, text: '协同支持' },
          { value: 13, text: '瑞币收入' },
          { value: 14, text: '演讲沙龙' },
        ],
        // 请求统计数据的函数
        statisticsFuncs: [],
        // 请求的定时器
        requestTimer: null,
        // 模块统计数据
        a: {
          admin_name: '',
          dataList: [
            {
              month: '',
              publish: 10,
              running: 3,
            },
            {
              month: '',
              publish: 10,
              running: 3,
            },
          ],
        },
        moduleStatisticList: [
          {
            name: 'strategyWork',
            nameText: '投顾策略[发布量(运行中)]',
            tips: '说明：以创建时间为基准统计当月发布的数量和运行中的数量',
            isLoaded: false,
            requestFunc: ssMonthly,
            tableData: [],
            tableColumns: [],
          },
          {
            name: 'investment',
            nameText: '投资计划[发布量(运行中)]',
            tips: '说明: 以创建时间为基准统计当月发布的数量和运行中的数量',
            isLoaded: false,
            requestFunc: SInvestmentMonthly,
            tableData: [],
            tableColumns: [],
          },
          {
            name: 'shortMessage',
            nameText: '短讯通',
            tips: '',
            isLoaded: false,
            requestFunc: sShortMsgMonthly,
            tableData: [],
            tableColumns: [],
          },
          {
            name: 'specialTask',
            nameText: '非常规工作(含营业部路演)',
            tips: '注: 数量统计包括路演中的营业部类别',
            isLoaded: false,
            requestFunc: sTaskMonthly,
            tableData: [],
            tableColumns: [],
          },
          {
            name: 'articleWriting',
            nameText: '文章撰写',
            tips: '',
            isLoaded: false,
            requestFunc: sArticleMontyly,
            tableData: [],
            tableColumns: [],
          },
          {
            name: 'lecture',
            nameText: '路演(不含营业部类型)',
            tips: '注: 数据不含申请者为营业部的',
            isLoaded: false,
            requestFunc: sLectureMontyly,
            tableData: [],
            tableColumns: [],
          },
          {
            name: 'ondutyMessage',
            nameText: '值班信息',
            tips: '',
            isLoaded: false,
            requestFunc: sOndutyMessageMonthly,
            tableData: [],
            tableColumns: []
          },
        ],
      }
    },
    watch: {
      tabNum() {
        if (this.tabNum === 1) {
          // 请求个模块统计
          this.loadModuleStatistics()
        }
      },
    },
    created() {
      // this.getShowDatetime()
    },
    mounted() {
      // this.timer = setInterval(() => {}, 1000)
      this.setCurrentYearDateRange()
      // this.queryTableData()
    },
    destroyed() {
      // clearInterval(this.timer)
    },
    methods: {
      loadModuleStatistics() {
        const para = {
          start: datetimeToStr(this.dateRange[0], true, ''),
          end: datetimeToStr(this.dateRange[1], true, ''),
          st: this.statisticsType,
        }
        this.moduleStatisticList.forEach(async (item) => {
          item.isLoaded = false
          const res = await item.requestFunc(para)
          // console.log(res)
          item.tableData = res.data.statistics_data
          item.tableColumns = res.data.table_columns
          item.isLoaded = true
          // console.log(item)
        })
      },

      getShowDatetime() {
        const date = new Date()
        let week
        switch (date.getDay()) {
          case 1:
            week = '星期一'
            break
          case 2:
            week = '星期二'
            break
          case 3:
            week = '星期三'
            break
          case 4:
            week = '星期四'
            break
          case 5:
            week = '星期五'
            break
          case 6:
            week = '星期六'
            break
          default:
            week = '星期日'
        }
        let year = date.getFullYear()
        let month = this.addZero(date.getMonth()+1);
        let days = this.addZero(date.getDate());
        let hours = this.addZero(date.getHours());
        let minutes = this.addZero(date.getMinutes());
        let seconds = this.addZero(date.getSeconds());
        let ndate = `${year}年${month}月${days}日 ${week} ${hours}:${minutes}:${seconds}`
        this.showDatetime = ndate
      },

      addZero(t) {
        return t < 10 ? '0' + t : t
      },

      // 查询统计表数据
      queryTableData() {
        const para = {
          start: datetimeToStr(this.dateRange[0], true, ''),
          end: datetimeToStr(this.dateRange[1], true, ''),
          st: this.statisticsType,
          ut: this.employeeType,
          sort: this.sortType
        }
        this.dataList = []
        this.emptyDataDescription = '数据统计中...'
        getDepartmentWorkStatistics(para)
          .then((res) => {
            // console.log(res);
            const data = res.data
            this.dataList = data.statistics_data
            this.tableTitle = data.title
            this.userCount = data.user_count
            this.emptyDataDescription = '点击上方查询获取统计数据'
          })
          .catch((err) => {
            this.emptyDataDescription = '查询统计数据失败！'
          })
        if (this.tabNum === 1) {
          this.loadModuleStatistics()
        }
      },

      // 查询当前年度日期数据
      setCurrentYearDateRange() {
        // 设置时间显示，从年度7月01日到下一年度6月30日
        const current = new Date()
        if (current.getMonth() + 1 >= 7) {
          // 显示今年到明年
          this.dateRange = [
            new Date(current.getFullYear(), 6, 1),
            new Date(current.getFullYear() + 1, 5, 30),
          ]
        } else {
          // 显示去年到今年
          this.dateRange = [
            new Date(current.getFullYear() - 1, 6, 1),
            new Date(current.getFullYear(), 5, 30),
          ]
        }
        // 查询数据
        this.queryTableData()
      },

    // 查询上一年度数据
    setForeYearDateRange(){
      const current = this.dateRange[0]
      this.dateRange = [new Date(current.getFullYear() - 1, 6, 1), new Date(current.getFullYear(), 5, 30)]
      // 查询数据
      this.queryTableData()
    },

    // 查询下一年度数据
    setNextYearDateRange(){
      const current = this.dateRange[0]
      this.dateRange = [new Date(current.getFullYear() + 1, 6, 1), new Date(current.getFullYear() + 2, 5, 30)]
      // 查询数据
      this.queryTableData()
    },
  }
}
</script>

<style scoped>
  .container {
    height: 150px;
    padding-top: calc(40vh - 75px);
  }
  .table-title {
    text-align: center;
    font-size: 20px;
    font-weight: 700;
    line-height: 30px;
  }
  .work-name {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 5px;
    margin-top: 16px;
  }
  .work-tip {
    font-size: 11px;
    color: #f34d37;
    margin-top: 3px;
  }
</style>
